package org.wlldTest.lesson8;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.dromara.easyai.config.RZ;
import org.dromara.easyai.function.Tanh;
import org.dromara.easyai.nerveCenter.ModelParameter;
import org.dromara.easyai.nerveCenter.NerveManager;
import org.dromara.easyai.nerveEntity.SensoryNerve;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Test {
    public static NerveManager nerveManager;

    public static void main(String[] args) throws Exception {
        init();
        study();
    }

    public static void init() throws Exception {
        nerveManager = new NerveManager(4, 3, 2, 1,
                new Tanh(), 0.01f, RZ.NOT_RZ, 0, 0, 0.9f, 2, false);
        nerveManager.init(true, true, true, null);
//        ModelParameter modelParameter = JSONObject.parseObject(Model.Model, ModelParameter.class);
//        nerveManager.insertDnnModel(modelParameter);
    }

    public static void test() throws Exception {
        Random random = new Random();
        Back back = new Back();
        for (int i = 0; i < 10; i++) {
            System.out.println("=======================");
            float x1 = random.nextFloat() * 0.1f;
            float y1 = random.nextFloat() * 0.1f + 0.1f;
            float a1 = x1 - 0.2f;
            float b1 = y1 + 0.3f;
            float x2 = x1 + 0.5f;
            float y2 = y1 + 0.5f;
            float a2 = x1 - 0.4f;
            float b2 = x2 - 0.6f;
            float[] parameter1 = new float[]{x1, y1, a1, b1};
            float[] parameter2 = new float[]{x2, y2, a2, b2};
            insertParameter(parameter2, false, null, back);
            System.out.println("v1:" + back.getV1());
            System.out.println("v2:" + back.getV2());
        }
    }

    public static void study() throws Exception {// 1,0
        Random random = new Random();
        Map<Integer, Float> E1 = new HashMap<>();//期望1
        Map<Integer, Float> E2 = new HashMap<>();//期望2
        E1.put(1, 1f);
        E2.put(2, 1f);
        for (int i = 0; i < 2000; i++) {
            System.out.println("======================" + i);
            float x1 = random.nextFloat() * 0.1f;
            float y1 = random.nextFloat() * 0.1f + 0.1f;
            float a1 = x1 - 0.2f;
            float b1 = y1 + 0.3f;
            float x2 = x1 + 0.5f;
            float y2 = y1 + 0.5f;
            float a2 = x1 - 0.4f;
            float b2 = x2 - 0.6f;
            float[] parameter1 = new float[]{x1, y1, a1, b1};
            float[] parameter2 = new float[]{x2, y2, a2, b2};
            insertParameter(parameter1, true, E1, null);
            insertParameter(parameter2, true, E2, null);
        }
        ModelParameter modelParameter = nerveManager.getDnnModel();
        String model = JSON.toJSONString(modelParameter);
        System.out.println(model);
    }

    public static void insertParameter(float[] parameter, boolean isStudy, Map<Integer, Float> E
            , Back back) throws Exception {
        List<SensoryNerve> sensoryNerves = nerveManager.getSensoryNerves();
        for (int i = 0; i < sensoryNerves.size(); i++) {
            sensoryNerves.get(i).postMessage(1, parameter[i], isStudy, E, back);
        }
    }
}
